const sConf = getApp().globalData
export default {
	/**
	 * 转换性别
	 */
	zipGender(e) {
		return e === 1 ? '男' : '女'
	},
	/**
	 * 去除null
	 */
	cutNull(e) {
		return Object.assign({}, ...Object.keys(e).map(key => (e[key] !==
			null ? {
				[key]: e[key]
			} : {})));
	},
	requestPostSync(host, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "POST",
				header: {
					'content-type': 'application/json'
				},
				data: data,
				success(res) {
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	authenticatorGetSync(host, token, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "GET",
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				data: data,
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	authenticatorPostSync(host, token, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "POST",
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				data: data,
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	authenticatorPutSync(host, token, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "PUT",
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				data: data,
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	authenticatorPatchSync(host, token, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "PATCH",
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				data: data,
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	authenticatorDeleteSync(host, token) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: host,
				method: "DELETE",
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	/**
	 * 注销登录
	 */
	syncRemoveLoginToken() {
		let key = sConf.env + '_login'
		uni.showModal({
			title: '退出登录',
			content: '是否要退出登录',
			success(r) {
				if (r.confirm) {
					uni.removeStorage({
						key: key,
						success: function(res) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					});
				}
			}
		})
	},
	/**
	 * 写入用户登录凭证-异步
	 * @param {Object} data 只支持原生类型、及能够通过 JSON.stringify 序列化的对象
	 */
	syncSetLoginToken(data) {
		let key = sConf.env + '_login'
		data['last'] = Date.now();
		console.log('保存用户登录信息->', data);
		uni.setStorage({
			key: key,
			data: data
		});
	},
	/**
	 * 获取用户登录凭证-同步
	 */
	syncHasLoginToken() {
		try {
			let key = sConf.env + '_login'
			let stamp = Date.now();
			let login = uni.getStorageSync(key)
			if (login && (stamp - login.last) < 12 * 3600 * 1000) {
				return login
			} else {
				uni.clearStorageSync();
				return null
			}
		} catch (err) {
			console.warn('加载登录信息失败=>', err);
			return null
		}
	},
	/**
	 * 匿名访问
	 */
	syncAnonymousRequest(host, method = "GET", data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: sConf.host + host,
				method: method,
				header: {
					'content-type': 'application/json'
				},
				data: data,
				success(res) {
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},
	/**
	 * 登陆后访问
	 */
	syncAuthenticatorRequest(host, method = "GET", token, data = {}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: sConf.host + host,
				method: method,
				header: {
					'content-type': 'application/json',
					'authorization': token
				},
				data: data,
				success(res) {
					if (res.data.rescode === 401) {
						uni.$emit('logout')
						reject(res.data)
					}
					if (res.data.rescode === 200) {
						resolve(res.data)
					} else {
						reject(res.data)
					}
				},
				fail(err) {
					reject({
						rescode: 999,
						resmsg: err
					})
				}
			})
		})
	},


}